/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.querysetting.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.querysetting.entity.QuerySetting;
import com.fosung.frame.modules.querysetting.service.QuerySettingService;

/**
 * 查询设置Controller
 * @author wu
 * @version 2017-08-31
 */
@Controller
@RequestMapping(value = "${adminPath}/querysetting/querySetting")
public class QuerySettingController extends BaseController {

	@Autowired
	private QuerySettingService querySettingService;
	
	@ModelAttribute
	public QuerySetting get(@RequestParam(required=false) String id) {
		QuerySetting entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = querySettingService.get(id);
		}
		if (entity == null){
			entity = new QuerySetting();
		}
		return entity;
	}
	
	@RequiresPermissions("querysetting:querySetting:view")
	@RequestMapping(value = {"list", ""})
	public String list(QuerySetting querySetting, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<QuerySetting> page = querySettingService.findPage(new Page<QuerySetting>(request, response), querySetting); 
		model.addAttribute("page", page);
		return "modules/querysetting/querySettingList";
	}

	@RequiresPermissions("querysetting:querySetting:view")
	@RequestMapping(value = "form")
	public String form(QuerySetting querySetting, Model model) {
		model.addAttribute("querySetting", querySetting);
		return "modules/querysetting/querySettingForm";
	}

	@RequiresPermissions("querysetting:querySetting:edit")
	@RequestMapping(value = "save")
	public String save(QuerySetting querySetting, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, querySetting)){
			return form(querySetting, model);
		}
		querySettingService.save(querySetting);
		addMessage(redirectAttributes, "保存查询设置成功");
		return "redirect:"+Global.getAdminPath()+"/querysetting/querySetting/?repage";
	}
	
	@RequiresPermissions("querysetting:querySetting:edit")
	@RequestMapping(value = "delete")
	public String delete(QuerySetting querySetting, RedirectAttributes redirectAttributes) {
		querySettingService.delete(querySetting);
		addMessage(redirectAttributes, "删除查询设置成功");
		return "redirect:"+Global.getAdminPath()+"/querysetting/querySetting/?repage";
	}
	
	@RequiresPermissions("partyassessment:partyAssessment:edit")
	@RequestMapping(value = "updateStart")
	public String updateStart(QuerySetting querySetting, RedirectAttributes redirectAttributes) {
		querySettingService.updateStart(querySetting);
		addMessage(redirectAttributes, "启动查询设置成功");
		return "redirect:"+Global.getAdminPath()+"/querysetting/querySetting/?repage";
	}
	
	@RequiresPermissions("partyassessment:partyAssessment:edit")
	@RequestMapping(value = "updateStop")
	public String updateStop(QuerySetting querySetting, RedirectAttributes redirectAttributes) {
		querySettingService.updateStop(querySetting);
		addMessage(redirectAttributes, "关闭查询设置成功");
		return "redirect:"+Global.getAdminPath()+"/querysetting/querySetting/?repage";
	}
}